<!DOCTYPE html>
<html>
<!--
  Copyright (c) 2013 The Chromium Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
-->
<head>
<title>IDB test that origins over quota aren't trapped Part 1 / 2</title>
<script type="text/javascript" src="common.js"></script>
<script>

function test() {
  indexedDBTest(prepareDatabase, startNewTransaction);
}

function prepareDatabase()
{
  db = event.target.result;
  objectStore = db.createObjectStore("test123");
  var len = 1024;
  data = Array(1+len).join("X");
  numTransactions = 0;
}

function startNewTransaction() {
  debug("Starting new transaction.");

  var trans = db.transaction(['test123'], 'readwrite');
  trans.onabort = unexpectedAbortCallback;
  trans.oncomplete = getQuotaAndUsage;
  var store = trans.objectStore('test123');
  request = store.put({x: data}, numTransactions);
  request.onerror = unexpectedErrorCallback;
}

function getQuotaAndUsage() {
  numTransactions++;
  navigator.webkitTemporaryStorage.queryUsageAndQuota(
    usageCallback, unexpectedErrorCallback);
}

function usageCallback(usage, quota) {
  debug("Transaction finished.");
  returnedUsage = usage;
  returnedQuota = quota;
  debug("Allotted quota is " + displaySize(returnedQuota));
  debug("LevelDB usage is " + displaySize(returnedUsage));
  if (returnedUsage < 5 * 1024)
    startNewTransaction();
  else
    done("Filled up " + returnedUsage);
}

function displaySize(bytes) {
  var k = bytes / 1024;
  var m = k / 1024;
  return bytes + " (" + k + "k) (" + m + "m)";
}

</script>
</head>
<body onLoad="test()">
<div id="status">Starting...</div>
</body>
</html>
